Implement strStr().
Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
回傳參數needle在參數haystack中,第一次出現的索引值,若沒有找到回傳-1。
若needle是空字串,回傳0。
Input: haystack = "hello", needle = "ll"
Output: 2
Input: haystack = "aaaaa", needle = "bba"
Output: -1
先設定邊界條件:
若找不到,回傳-1。
若needle是空字串,回傳0。
依序比對haystack中字元與needle的第一個字元是否符合,若有符合,比對接下來的字元(長度以needle為基準),是否跟needle符合。
function strStr(haystack = '', needle) {
  if (!!needle) {
    let neeLen = needle.length;
    let searchIdx = haystack.length - neeLen + 1;
    for (let i = 0; i < searchIdx; i++) {
      if (haystack.charAt(i) === needle.charAt(0)) {
        if (haystack.substring(i, i + neeLen) === needle) return i;
      }
    }
    return -1;
  }
  return 0;
}